var _cal = new Object();
_cal.curYear = 0;
_cal.curMonth = 0;
_cal.curDay = 0;
_cal.curHour = 0;
_cal.curMinute = 0;
_cal.curSecond = 0;
_cal.calType = 1;  //1-date, 2-time, 3-datetime
_cal.curTimeSet = 1;
_cal.ctrlObj = null;
_cal.isShow = false;
_cal.olddc = null;

_cal.getDtStr = function(dt) {
  return _cal.getYmdStr(dt.getFullYear(), (dt.getMonth()+1), dt.getDate());
}
_cal.getYmdStr = function(y, m, d) {
  return y + "-" + (m<10?"0":"") + m + "-" + (d<10?"0":"") + d;
}
_cal.getHmsStr = function(h, m, s) {
  return (h<10?"0":"") + h + ":" + (m<10?"0":"") + m + ":" + (s<10?"0":"") + s;
}

_cal.init = function() {
  var arr = new Array();
  arr.push('<div id="div_cal" onclick="_cal.clkCancel(event);" style="display:none; position:absolute; z-index:100; height:172px; background-color:#d4d0c8;">');
  if(document.all) arr.push('  <div style="position:absolute; z-index:-1; width:99%; height:99%;"><iframe scrolling=no frameborder=0 width=100% height=100%></iframe></div>');
  arr.push('  <table align="center" bgcolor="#d4d0c8" cellpadding="0" cellspacing="1">'
        + '    <tr>'
        + '      <td id="tdCalDate" valign="top"></td>'
        + '      <td id="tdCalTime" valign="top"></td>'
        + '    </tr>'
        + '  </table>'
        + '</div>');
  //arr.push('<img src="/images/date.jpg"></div>')
  document.write(arr.join(""));

  _cal.initDate();
  _cal.initTime();
}

_cal.initDate = function() {
  var i, j;
  var week = Array("日","一","二","三","四","五","六");
  var dt = new Date();

  var s = "<table style='font-size:12px;'>"
        + "  <tr align=center>"
        + "    <td height=23><font color='red' style='cursor:pointer;' onclick='_cal.prevMonth();'>◀</font></td>"
        + "    <td><select id='selYear' onchange='_cal.updateDate();'>";
      for(i=1931; i<=2100; i++)
        s += "<option value="+i+">"+i+"年</option>";
      s += "</select></td>"
        + "    <td><select id='selMonth' onchange='_cal.updateDate();'>";
      for(i=1; i<=12; i++)
        s += "<option value="+i+">"+i+"月</option>";
      s += "</select></td>"
        + "    <td><font color='red' style='cursor:pointer;' onclick='_cal.nextMonth();'>►</font></td>"
        + "  </tr>"
        + "  <tr align=center>"
        + "    <td colspan=4 style='border: #aaaaaa 1px solid;'>"
        + "      <table id='tbCalDate' width='100%' border='0' cellpadding='0' cellspacing='0' style='font-size:12px;'><tr align=center>";
      for(i=0; i<7; i++)
        s += "<td style='color:#7D1F00;' height=18>"+week[i]+"</td>"
      s += "</tr>";
      for(j=0; j<6; j++) {
        s += "<tr align=center>";
        for(i=0; i<7; i++)
          s += "<td style='cursor:pointer;' onmouseover='this.bgColor=\"#c7e9ef\";' onmouseout='this.bgColor=\"#d4d0c8\";' onclick='_cal.setSelDate(this);' height=16><font style='font-size:12px;'></font></td>";
        s += "</tr>";
      }
      s += "</table>"
        + "    </td>"
        + "  </tr>"
        + "  <tr align=center>"
        + "    <td colspan=4>"
        + "      <table width='90%' style='font-size:12px;'>"
        + "        <tr align=center>"
        + "          <td style='cursor:pointer;' onclick='_cal.setSelDate(this);' align=left><font id=ftToday color=green>今天:" + _cal.getDtStr(dt) + "</font></td>"
        + "          <td style='cursor:pointer;' onclick='_cal.setSelDate(this);'><font color=blue>清空</font></td>"
        + "        </tr>"
        + "      </table>"
        + "    </td>"
        + "  </tr>"
        + "</table>";
  document.getElementById("tdCalDate").innerHTML = s;

  var ft = document.getElementById("ftToday");
  if(ft) {
    ft.year = dt.getFullYear();
    ft.month = dt.getMonth()+1;
    ft.day = dt.getDate();
  }
}

_cal.initTime = function() {
  var s = "<table width=190 style='font-size:12px;'>"
        + "  <tr align=center style='font-size:14px; font-weight:bold;'>"
        + "    <td id=ftDate width=95 height=23></td>"
        + "    <td align=left><font id=ftHour style='cursor:pointer;' onclick='_cal.selTime(1);'>00</font>:<font id=ftMinute style='cursor:pointer;' onclick='_cal.selTime(2);'>00</font>:<font id=ftSecond style='cursor:pointer;' onclick='_cal.selTime(3);'>00</font></td>"
        + "  </tr>"
        + "  <tr align=center>"
        + "    <td colspan=2 style='border: #aaaaaa 1px solid;' id='tbCalTime'></td>"
        + "  </tr>"
        + "  <tr align=center>"
        + "    <td colspan=2>"
        + "      <table width='80%' cellpadding='0' cellspacing='0' style='font-size:12px;'>"
        + "        <tr align=center>"
        + "          <td><input type=button style='border: #888888 1px solid; width:50px; height:18px;' value='确定' onclick='_cal.selOk();'></td>"
        + "          <td><input type=button style='border: #888888 1px solid; width:50px; height:18px;' value='取消' onclick='_cal.selCancel();'></td>"
        + "        </tr>"
        + "      </table>"
        + "    </td>"
        + "  </tr>"
        + "</table>";
  document.getElementById("tdCalTime").innerHTML = s;
}

_cal.nextMonth = function() {
  var selYear = document.getElementById("selYear");
  var selMonth = document.getElementById("selMonth");

  var m = parseInt(selMonth.value);
  if(m<12) {
    selMonth.value = m+1;
  } else if(selYear.selectedIndex<selYear.options.length-1) {
    selYear.value = parseInt(selYear.value)+1;
    selMonth.value = 1;
  }
  _cal.updateDate();
}

_cal.prevMonth = function() {
  var selYear = document.getElementById("selYear");
  var selMonth = document.getElementById("selMonth");

  var m = parseInt(selMonth.value);
  if(m>1) {
    selMonth.value = m-1;
  } else if(selYear.selectedIndex>0) {
    selYear.value = parseInt(selYear.value)-1;
    selMonth.value = 12;
  }
  _cal.updateDate();
}

_cal.setSelDate = function(td) {
  var ft = td.childNodes[0];
  _cal.curYear = parseInt(ft.year);
  _cal.curMonth = parseInt(ft.month);
  _cal.curDay = parseInt(ft.day);
  if(_cal.calType==1) {
    _cal.selOk();
  } else {
    var selYear = document.getElementById("selYear");
    var selMonth = document.getElementById("selMonth");

    if(_cal.curYear>0) {
      document.getElementById("ftDate").innerHTML =  _cal.getYmdStr(_cal.curYear, _cal.curMonth, _cal.curDay);
      if(_cal.curYear!=parseInt(selYear.value)) selYear.value = _cal.curYear;
      if(_cal.curMonth!=parseInt(selMonth.value)) selMonth.value = _cal.curMonth;
    } else {
      document.getElementById("ftDate").innerHTML = "";
      _cal.curHour = 0;
      _cal.curMinute = 0;
      _cal.curSecond = 0;
      document.getElementById("ftHour").innerHTML = "00";
      document.getElementById("ftMinute").innerHTML = "00";
      document.getElementById("ftSecond").innerHTML = "00";
      _cal.updateTime();
    }
    _cal.updateDate();
  }
}

_cal.updateDate = function() {
  var tbCalDate = document.getElementById("tbCalDate")
  var selYear = document.getElementById("selYear");
  var selMonth = document.getElementById("selMonth");

  var bPast = true, y, m ,d;
  var today = new Date();
  today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
  var dt = new Date(selYear.value, selMonth.value-1, 1);
  var n = dt.getDay();
  dt = new Date(dt.getFullYear(), dt.getMonth(), 1-n);
  var r, c, ft, color, t;
  for(var i=0;i<42;i++) {
    r = parseInt(i/7) + 1;
    c = i%7;
    ft = tbCalDate.rows[r].cells[c].childNodes[0];

    y = dt.getFullYear();
    m = dt.getMonth()+1;
    d = dt.getDate();
    n = dt.getDay();
    if(d==1) bPast = !bPast

    t = d;
    if(y==_cal.curYear && m==_cal.curMonth && d==_cal.curDay) {
      color = "blue";
      t = "<u>" + d + "</u>";
    } else if(today.getFullYear()==y && today.getMonth()==m-1 && today.getDate()==d)
      color = "green";
    else if(bPast)
      color = "gray";
    else if(n==0 || n==6)
      color = "red";
    else
      color = "black";

    ft.innerHTML = t;
    ft.color = color;
    ft.year = y;
    ft.month = m;
    ft.day = d;

    dt = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate()+1);
  }
}

_cal.selTime = function(id) {
  var ftHour = document.getElementById("ftHour");
  var ftMinute = document.getElementById("ftMinute");
  var ftSecond = document.getElementById("ftSecond");

  _cal.curTimeSet = id;
  ftHour.color = _cal.curTimeSet==1?"red":"black";
  ftHour.style.textDecorationUnderline = _cal.curTimeSet==1?"underline":"";
  ftMinute.color = _cal.curTimeSet==2?"red":"black";
  ftMinute.style.textDecorationUnderline = _cal.curTimeSet==2?"underline":"";
  ftSecond.color = _cal.curTimeSet==3?"red":"black";
  ftSecond.style.textDecorationUnderline = _cal.curTimeSet==3?"underline":"";
  _cal.updateTime();
}

_cal.setSelTime = function(td) {
  var ftHour = document.getElementById("ftHour");
  var ftMinute = document.getElementById("ftMinute");
  var ftSecond = document.getElementById("ftSecond");

  var ft = td.childNodes[0];
  var tt = ft.getAttribute("tt");
  if(tt!="") {
    var n = parseInt(tt);
    if(_cal.curTimeSet==1) {
      _cal.curHour = n;
      ftHour.innerHTML = (n<10?"0":"") + n;
    } else if(_cal.curTimeSet==2) {
      _cal.curMinute = n;
      ftMinute.innerHTML = (n<10?"0":"") + n;
    } else if(_cal.curTimeSet==3) {
      _cal.curSecond = n;
      ftSecond.innerHTML = (n<10?"0":"") + n;
    }
    _cal.updateTime();
  }
}

_cal.updateTime = function() {
  var t, color;
  var n = 10, nn = 60, hh = 16;
  if(_cal.curTimeSet==1) {
    n = 6;
    nn = 24;
    hh = 24;
  }

  var s = "<table width='100%' height=114 border='0' cellpadding='0' cellspacing='0' style='font-size:12px;'>"
        + "  <tr>"
        + "    <td colspan="+n+" style='color:#7D1F00; padding-left:15px' height=18>请选择" + (_cal.curTimeSet==1?"小时":(_cal.curTimeSet==2?"分":"秒")) + "</td>"
        + "  </tr>";

  for(var i=0;i<nn;i++) {
    t = i;
    if((_cal.curTimeSet==1 && _cal.curHour==i) || (_cal.curTimeSet==2 && _cal.curMinute==i) || (_cal.curTimeSet==3 && _cal.curSecond==i)) {
      color = "blue";
      t = "<u>" + i + "</u>";
    } else
      color = "black";

    if(i%n==0) s += "<tr align=center>";

    s += "<td style='cursor:pointer;' onmouseover='this.bgColor=\"#c7e9ef\";' onmouseout='this.bgColor=\"#d4d0c8\";' onclick='_cal.setSelTime(this);' height="+hh+"><font style='font-size:12px;' color='"+color+"' tt="+i+">"+t+"</font></td>";

    if(i%n==(n-1))  s += "</tr>";

  }

  s += "</table>";
  document.getElementById("tbCalTime").innerHTML = s;
}

_cal.setCurDate = function(fdt, type) {
  var dateReg;
  if(type=="time") {
    _cal.calType = 2;
    dateReg = /^(\d{1,2}):(\d{1,2}):(\d{1,2}).*$/;
  } else if(type=="datetime") {
    _cal.calType = 3;
    dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}).*$/;
  } else {
    _cal.calType = 1;
    dateReg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}).*$/;
  }

  if(dt!="") {
    var dd = fdt.match(dateReg);
    if(dd) {
      if(_cal.calType==1 || _cal.calType==3) {
        _cal.curYear = parseInt(dd[1], 10);
        _cal.curMonth = parseInt(dd[3], 10);
        _cal.curDay = parseInt(dd[4], 10);
        if(isNaN(_cal.curYear) || _cal.curYear<1960 || _cal.curYear>2050) _cal.curYear = 0;
        if(isNaN(_cal.curMonth) || _cal.curMonth<1 || _cal.curMonth>12) _cal.curMonth = 1;
        if(isNaN(_cal.curDay) || _cal.curDay<1 || _cal.curDay>31) _cal.curDay = 1;
      }
      if(_cal.calType==2 || _cal.calType==3) {
        var nn = _cal.calType==2?0:4;
        _cal.curHour = parseInt(dd[nn+1], 10);
        _cal.curMinute = parseInt(dd[nn+2], 10);
        _cal.curSecond = parseInt(dd[nn+3], 10);
        if(isNaN(_cal.curHour) || _cal.curHour<0 || _cal.curHour>23) _cal.curHour = 0;
        if(isNaN(_cal.curMinute) || _cal.curMinute<0 || _cal.curMinute>59) _cal.curMinute = 0;
        if(isNaN(_cal.curSecond) || _cal.curSecond<0 || _cal.curSecond>59) _cal.curSecond = 0;
      }
    }
  }

  var selYear = document.getElementById("selYear");
  var selMonth = document.getElementById("selMonth");
  if(_cal.curYear>0) {
    selYear.value = _cal.curYear;
    selMonth.value = _cal.curMonth;
  } else {
    var dt = new Date();
    selYear.value = dt.getFullYear();
    selMonth.value = dt.getMonth()+1;
  }
  _cal.updateDate();

  document.getElementById("ftHour").innerHTML = (_cal.curHour<10?"0":"") + _cal.curHour;
  document.getElementById("ftMinute").innerHTML = (_cal.curMinute<10?"0":"") + _cal.curMinute;
  document.getElementById("ftSecond").innerHTML = (_cal.curSecond<10?"0":"") + _cal.curSecond;
  _cal.selTime(1);

  var ftDate = document.getElementById("ftDate");
  if(_cal.calType==3 && _cal.curYear>0) {
    ftDate.innerHTML = _cal.getYmdStr(_cal.curYear, _cal.curMonth, _cal.curDay);
  } else {
    ftDate.innerHTML = "";
  }
  ftDate.width = _cal.calType==2?25:95;
  document.getElementById("tdCalDate").style.display = _cal.calType==2?"none":"";
  document.getElementById("tdCalTime").style.display = _cal.calType==1?"none":"";
}

_cal.selOk = function() {
  var ret = "";
  if(_cal.calType==1) {
    if(_cal.curYear>0)
      ret = _cal.getYmdStr(_cal.curYear, _cal.curMonth, _cal.curDay);
    else
      ret = "";
  } else if(_cal.calType==2) {
    ret = _cal.getHmsStr(_cal.curHour, _cal.curMinute, _cal.curSecond);
  } else {
    if(_cal.curYear>0) {
      ret = _cal.getYmdStr(_cal.curYear, _cal.curMonth, _cal.curDay);
      ret += " " + _cal.getHmsStr(_cal.curHour, _cal.curMinute, _cal.curSecond);
    } else {
      ret = "";
    }
  }

  if(_cal.ctrlObj) {
    _cal.ctrlObj.value = ret;
    if(_cal.ctrlObj.onchange) _cal.ctrlObj.onchange();
  }
  _cal.selCancel();
  if(typeof(calOk)=="function") calOk(ret);
}

_cal.selCancel = function() {
  var div = document.getElementById("div_cal");
  if(div) div.style.display = "none";
  _cal.isShow = false;
  document.onclick = _cal.olddc;
}

_cal.clkCancel = function(e) {
  e.cancelBubble = true;
  e.returnValue = false;
  e.cancel = true;
  return false;
}
_cal.dmclick = function(e) {
  if(_cal.isShow) {
    var el;
    if(window.event)
      el = event.srcElement;
    else
      el = e.target;
    if(_cal.ctrlObj != el) {
      _cal.selCancel();
    }
  }
  //if(_cal.olddc) _cal.olddc(e);
}

_cal.show = function(cobj, caltype) {
  var el = cobj;
  var showy = 0, showx = 0;
  while(el) {
    showy += el.offsetTop;
    showx += el.offsetLeft;
    if(el.tagName=="DIV") {
      showy -= el.scrollTop
      showx -= el.scrollLeft
    }
	el = el.offsetParent;
  }
  showy += cobj.offsetHeight;

  var wid = 160;
  if(caltype=="time")
    wid = 192;
  else if(caltype=="datetime")
    wid = 348;

  _cal.ctrlObj = cobj;
  _cal.setCurDate(cobj.value, caltype);
  var div = document.getElementById("div_cal");
  if(div) {
    div.style.left = showx+"px";
    div.style.top = showy+"px";
    //div.style.width = wid+"px";
    div.style.display = "";
  }
  _cal.isShow = true;
  //_cal.olddc = document.onclick;
  document.onclick = _cal.dmclick;
}

var cal = _cal.show;
_cal.init();